package com.elong.businesstravel.modules.home;

import java.util.concurrent.TimeUnit;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ZoomControls;

import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.utils.CoordinateConversion;
import com.android.support.jhf.utils.MobileUtils;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMapClickListener;
import com.baidu.mapapi.map.BaiduMap.OnMapLoadedCallback;
import com.baidu.mapapi.map.BaiduMap.OnMapStatusChangeListener;
import com.baidu.mapapi.map.BaiduMap.OnMarkerClickListener;
import com.baidu.mapapi.map.InfoWindow.OnInfoWindowClickListener;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.baidusdk.location.BaiduLocation;
import com.elong.businesstravel.base.baidusdk.map.BaiduMapView;
import com.elong.businesstravel.base.baseactivity.BaseActivity;
import com.elong.businesstravel.base.baseactivity.BaseMapActivity;
import com.elong.businesstravel.base.view.TitleNavBarView;
import com.elong.businesstravel.entitys.HotelDocs;
import com.elong.businesstravel.protocol.ProtocolDef;

/**
 * 酒店位置
 * 
 * @author lenovo
 * 
 */
public class HotelLocationMapActivity extends BaseMapActivity {

	private BitmapDescriptor mBitmapDescriptor;
	private InfoWindow mInfoWindow;

	private String mGoogleLng;
	private String mGoogleLat;
	private String mHotelName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mSavedInstanceState = savedInstanceState;
		setContentView(R.layout.activity_hotel_location_map);

		init();
	}

	@Override
	protected void init() {
		if (null == mSavedInstanceState) {
			mGoogleLat = getIntent().getStringExtra("GoogleLat");
			mGoogleLng = getIntent().getStringExtra("GoogleLng");
			mHotelName = getIntent().getStringExtra("HotelName");
		} else {
			mGoogleLat = mSavedInstanceState.getString("GoogleLat");
			mGoogleLng = mSavedInstanceState.getString("GoogleLng");
			mHotelName = mSavedInstanceState.getString("HotelName");
		}

		mMapView = (MapView) findViewById(R.id.mapView);
		initBaiduMap(mMapView);

		mBitmapDescriptor = BitmapDescriptorFactory
				.fromResource(R.drawable.ic_gcoding);

		mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {
			public boolean onMarkerClick(final Marker marker) {

				addPopupOverlay(mGoogleLng, mGoogleLat, mHotelName);

				return true;
			}
		});

		mBaiduMap.setOnMapLoadedCallback(new OnMapLoadedCallback() {

			@Override
			public void onMapLoaded() {

				addOverlay(mGoogleLng, mGoogleLat, mHotelName);

				addPopupOverlay(mGoogleLng, mGoogleLat, mHotelName);

				LatLng ll = getGeoPoint(mGoogleLng, mGoogleLat);
				MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
				mBaiduMapView.getBaiduMap().animateMapStatus(u);
			}
		});

		mBaiduMap.setOnMapStatusChangeListener(new OnMapStatusChangeListener() {

			@Override
			public void onMapStatusChangeStart(MapStatus arg0) {
				// DebugLog.logI("onMapStatusChangeStart : " + arg0.toString());
				if (null != mBaiduMap) {
					mBaiduMap.hideInfoWindow();
				}
			}

			@Override
			public void onMapStatusChangeFinish(MapStatus arg0) {

				if (null != mBaiduMap && mShowPopupFlag) {
					addPopupOverlay(mGoogleLng, mGoogleLat, mHotelName);
				}
				// DebugLog.logI("onMapStatusChangeFinish : " +
				// arg0.toString());
			}

			@Override
			public void onMapStatusChange(MapStatus arg0) {
				// DebugLog.logI("onMapStatusChange : " + arg0.toString());
			}
		});

		mBaiduMap.setOnMapClickListener(new OnMapClickListener() {

			@Override
			public boolean onMapPoiClick(MapPoi arg0) {
				return false;
			}

			@Override
			public void onMapClick(LatLng arg0) {
				if (null != mBaiduMap) {
					mShowPopupFlag = false;
					mBaiduMap.hideInfoWindow();
				}
			}
		});

		Button locButton = (Button) findViewById(R.id.locButton);
		locButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				baiduLocation();
			}
		});
		setTitleNavBar();
	}

	@Override
	protected void setTitleNavBar() {
		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage("酒店位置");
		titleNavBarView.setLeftDrawableImage(R.drawable.btn_title_back);
		titleNavBarView.setLeftButtonImageString(0, "返回", R.color.text_blue);
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}

	private void addPopupOverlay(String gg_lng, String gg_lat, String hotelName) {

		LatLng geoPoint = getGeoPoint(gg_lng, gg_lat);

		View view = LayoutInflater.from(mContext).inflate(
				R.layout.view_hotel_map_name, null);
		TextView hotelNameTextView = (TextView) view
				.findViewById(R.id.hotelNameTextView);
		hotelNameTextView.setText(hotelName);
		hotelNameTextView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});

		Point p = mBaiduMap.getProjection().toScreenLocation(geoPoint);
		p.y -= 20;
		LatLng llInfo = mBaiduMap.getProjection().fromScreenLocation(p);

		OnInfoWindowClickListener listener = new OnInfoWindowClickListener() {
			public void onInfoWindowClick() {
				// LatLng llNew = new LatLng(ll.latitude + 0.005,
				// ll.longitude + 0.005);
				// marker.setPosition(llNew);
				// mBaiduMap.hideInfoWindow();
			}
		};

		mInfoWindow = new InfoWindow(view, llInfo, listener);
		mBaiduMap.showInfoWindow(mInfoWindow);
		mShowPopupFlag = true;
	}

	private LatLng getGeoPoint(String gg_lng, String gg_lat) {
		double baiduLat = 0;
		double baiduLng = 0;
		try {
			double googleLat = Double.parseDouble(gg_lat);
			double googleLng = Double.parseDouble(gg_lng);
			double[] lngLat = CoordinateConversion.gg2bd(googleLat, googleLng);
			baiduLat = lngLat[1];
			baiduLng = lngLat[0];
		} catch (Exception e) {
		}
		/**
		 * 准备overlay 数据
		 */
		LatLng p1 = new LatLng(baiduLat, baiduLng);

		return p1;
	}

	private void addOverlay(String gg_lng, String gg_lat, String hotelName) {

		LatLng p1 = getGeoPoint(gg_lng, gg_lat);

		OverlayOptions ooA = new MarkerOptions().position(p1)
				.icon(mBitmapDescriptor).title(hotelName).zIndex(9)
				.draggable(false);
		mBaiduMap.addOverlay(ooA);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putString("GoogleLat", mGoogleLat);
		outState.putString("GoogleLng", mGoogleLng);
		outState.putString("HotelName", mHotelName);

		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		if (null != savedInstanceState) {
			mGoogleLat = savedInstanceState.getString("GoogleLat");
			mGoogleLng = savedInstanceState.getString("GoogleLng");
			mHotelName = savedInstanceState.getString("HotelName");
		}
	}
}
